﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using PBDesk.Utilities;
using System.Collections.Specialized;

namespace PBDesk.Framework.WebUI
{
    public static class CookieManager
    {
       

        public static void WriteCookie(string cookieName, string cookieValue, DateTime expire)
        {
            if (IsCookieEnabled())
            {
                HttpCookie cookie = new HttpCookie(cookieName);
                cookie.Value = cookieValue;
                cookie.Expires = expire;
                WriteCookie(cookie);
            }
        }
        public static void WriteCookie(string cookieName, string cookieValue)
        {
            if (IsCookieEnabled())
            {
                HttpCookie cookie = new HttpCookie(cookieName);
                cookie.Value = cookieValue;
                WriteCookie(cookie);
            }
        }

        public static void WriteCookie(string parentCookineName, string cookieName, string cookieValue)
        {
            if (IsCookieEnabled() && cookieName.IsNotNullAndNotEmpty())
            {
                //NameValueCollection pair = new NameValueCollection() { {cookieName, cookieValue}};
                //pair.Add(cookieName, cookieValue);
                WriteCookie(parentCookineName, new NameValueCollection() { { cookieName, cookieValue } });
            }
        }

        public static void WriteCookie(string parentCookineName, NameValueCollection keyValues)
        {
            if (IsCookieEnabled())
            {
                HttpCookie parentCookie = GetCookie(parentCookineName);
                if (parentCookie == null)
                    parentCookie = new HttpCookie(parentCookineName);

                if (keyValues != null && keyValues.Count > 0)
                {
                    foreach (KeyValuePair<string,string>  pair in keyValues)
                    {
                        parentCookie.Values[pair.Key] = pair.Value;
                    }
                }
                WriteCookie(parentCookie);

            }
        }
        public static void WriteCookie(HttpCookie cookie)
        {
            if (IsCookieEnabled())
            {
                if (cookie != null && cookie.Name.IsNotNullAndNotEmpty())
                {
                    HttpContext.Current.Response.Cookies.Add(cookie);

                    //if (cookie.HasKeys)
                    //{
                    //}
                    //else
                    //{
                    //    if (cookie.Value.IsNotNullAndNotEmpty())
                    //    {
                    //        HttpContext.Current.Response.Cookies.Add(cookie);
                    //    }
                           
                    //}
                }
                
            }
        }

        public static string GetCookieValue(string parentCookieName, string childCookieName)
        {
            if (IsCookieEnabled())
            {
                HttpCookie cookie = GetCookie(parentCookieName);
                if (cookie != null && cookie.HasKeys)
                {
                    if (childCookieName.IsNotNullAndNotEmpty())
                    {
                        return cookie.Values[childCookieName];
                    }
                }
            }
            return string.Empty;
        }
        public static string GetCookieValue(string cookieName)
        {
            if (IsCookieEnabled())
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
                if (cookie != null)
                {
                    if (cookie.HasKeys == false)
                    {
                        return cookie.Value;
                    }
                    else
                    {
                        throw new Exception("Cookie is MultiValued Cookie");
                    }
                }
                else
                {   // todo : log exception
                    //throw new Exception("cookie with name " + cookieName + " is not defined");
                }
            }
            return string.Empty;

        }
       
        public static bool IsCookieEnabled()
        {
            if (HttpContext.Current != null)
            {
                if (HttpContext.Current.Request.Cookies != null)
                    return true;
            }
            return false;
        }

        private static HttpCookie GetCookie(string cookieName)
        {
            HttpCookie cookie = null;
            if (IsCookieEnabled() && cookieName.IsNotNullAndNotEmpty())
            {
                cookie = HttpContext.Current.Request.Cookies[cookieName];
            }
            return cookie;
        }
    }
}
